local game = require "club.threecard.blackjack"

local bet_operator = game.ui.element()

local log = game.logging.get("bet_operator")

bet_operator.store_watchers = 
{
    ingame = 
    {
        ["阶段变更"] = function(self, store, state, args, isInitialize)
            if isInitialize then
                return;
            end

            if state.phase == state.PHASE.IDLE or state.phase == state.PHASE.PREPARE then
                self:on_bet_phase();
            else
                self:on_game_phase();
            end
        end,

        ["更新下注档位"] = function(self, store, state, args, isInitialize)
            self:update_bet_value();
        end,

        ["最大注"] = function(self, store, state, args, isInitialize)
            if isInitialize then
                self:max_bet(false);
                return;
            end

            self:max_bet(true);
        end,

        ["改变下注"] = function(self, store, state, args, isInitialize)
            self:max_bet(false);
            self:update_bet_value();
        end,
    }
}

function bet_operator:ctor()
    self.app = game;
    self.res = "club.threecard.blackjack/ui/ingame.b:bet_operator"
end

function bet_operator:on_injected(bind)
    bind("button.click", self.btn_add, self.add_ivk);
    bind("button.click", self.btn_sub, self.sub_ivk);
    bind("button.click", self.btn_bet, self.bet_ivk);

    game.utility.button_style.highlight_scale(self.btn_add);
    game.utility.button_style.highlight_scale(self.btn_sub);
    game.utility.button_style.highlight_scale(self.btn_bet);
end

function bet_operator:on_bet_phase()
    self.btn_bet.gameObject:SetActive(true);
    self.btn_add.gameObject:SetActive(true);
    self.btn_sub.gameObject:SetActive(true);
end

function bet_operator:on_game_phase()
    self.btn_bet.gameObject:SetActive(false);
    self.btn_add.gameObject:SetActive(false);
    self.btn_sub.gameObject:SetActive(false);
end

function bet_operator:bet_ivk()
    local amount = game.store.ingame.state.bet_value;
    game.kbe.player():bet(amount);
end

function bet_operator:add_ivk()
    game.store.ingame.on_bet_change({changed = 1});
end

function bet_operator:sub_ivk()
    game.store.ingame.on_bet_change({changed = -1});
end

function bet_operator:update_bet_value()
    self.bet_value_txt.text = game.i18n.number(game.store.ingame.state.bet_value, "chip3");
end

function bet_operator:max_bet(b)
    self.bet_max_tip:SetActive(b);
end

return bet_operator